/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2012 Gary Paluk, http://www.plugin.io
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

package io.plugin.blib.gfx 
{
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author Gary Paluk - http://www.plugin.io
	 */
	public class BlibAnimatedTile extends BlibTile 
	{
		
		private var _animation: BlibAnimation;
		private var _frameProgression: Number;
		private var _enabled: Boolean;
		
		public function BlibAnimatedTile( animation: BlibAnimation ) 
		{
			super( animation.getTileAt( 0 ).tileSet );
			
			_animation = animation;
			_frameProgression = 0;
			_enabled = true;
		}
		
		override public function update():void 
		{
			
			if ( enabled )
			{
				var animSpeed: Number = _animation.speed;
				if ( animSpeed != 0 )
				{
					
					_frameProgression += ( animSpeed * ( getDeltaTime() * 0.001 ) );
					
					if ( animSpeed > 0 )
					{
						if ( _frameProgression >= _animation.frames.size() )
						{
							_frameProgression = 0;
						}
					}
					else
					{
						if ( _frameProgression < 0 )
						{
							_frameProgression = _animation.frames.size();
						}
					}
					super.update();
				}
			}
		}
		
		override public function get tileSet():BlibTileSet 
		{
			return _animation.getTileAt( int( _frameProgression ) ).tileSet;
		}
		
		override public function set tileSet(value:BlibTileSet):void 
		{
			super.tileSet = value;
		}
		
		
		private var _lastTime: int = getTimer();
		private function getDeltaTime(): int
		{
			var delta: int = getTimer() - _lastTime;
			_lastTime = getTimer();
			return delta;
		}
		
		public function get animation():BlibAnimation 
		{
			return _animation;
		}
		
		public function set BlibAnimation(value:BlibAnimation):void 
		{
			_animation = value;
		}
		
		override public function get tileX():int 
		{
			return _animation.getTileAt( _frameProgression ).tileX;
		}
		
		override public function set tileX(value:int):void 
		{
			_animation.getTileAt( _frameProgression ).tileX = value;
		}
		
		override public function get tileY():int 
		{
			return _animation.getTileAt( _frameProgression ).tileY;
		}
		
		override public function set tileY(value:int):void 
		{
			_animation.getTileAt( _frameProgression ).tileY = value;
		}
		
		public function get frameProgression():Number 
		{
			return _frameProgression;
		}
		
		public function set frameProgression(value:Number):void 
		{
			_frameProgression = value;
		}
		
		public function get enabled():Boolean 
		{
			return _enabled;
		}
		
		public function set enabled(value:Boolean):void 
		{
			_enabled = value;
		}
		
		
	}

}